package com.lcf.app.basic.async;

import com.lcf.app.basic.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;

/**
 * @author ChenFei
 * @date 2021/11/11
 */
@Slf4j
@RestController
@RequestMapping("async")
public class AsyncController {

    @Resource
    private AsyncService asyncService;

    @GetMapping("init")
    public void init() {
        asyncService.method1();
        log.info("Controller代码执行完毕！");
    }

    @GetMapping("future")
    public String future() throws InterruptedException {

        CompletableFuture<User> method2 = asyncService.method2("ZhangSan");
        CompletableFuture<User> method3 = asyncService.method3("LiSi");

        CompletableFuture.allOf(method2, method3).join();//等待执行完毕

        return "futrue";
    }

}
